<?php

/**
 * Internationalization class
 * Experiment only, use with caution
 * 
 * @package OWL
 * @subpackage Main
 * @author KeiMOon
 * @since 1.0
 */
define("LANGUAGE_EXTENSION", "lang");
define("LANG_OPEN_DELIMITER", "<<<");
define("LANG_CLOSE_DELIMITER", ">>>");
define("LANG_MIDDLE_DELIMITER", "::");

class Owl_i18n {
    /**
     * Cache, reduce the number of times to read file as much as possible
     *
     * @var Array
     */
    static private $__cache = array();

    static private $__file_cache;

    /**
     * Get current language
     *
     * @return string
     */
    function get_language() {
        $user = Owl_User::get_instance();
        if ($user->get_user_lang() != false) {
            return $user->get_user_lang();
        }
        return get_option("language");
    }

    /**
     * Translate function, do not use this, use the alias __() is much easier and
     * nicer
     *
     * @param string $string Original string
     * @return string Translated string
     */
    function translate($string, $ulang = false, $force = false) {
        $translated = $this->from_cache($string);
        if ($translated && !$force) {
            return $translated;
        }
        $language = $this->get_language();
        if ($this->language_exist($language) || $language == 'en') {
            $translated =  $this->from_file($string, $language);
            if ($translated === false && $ulang) {
                $translated = $this->from_user_lang($string, $language, $force);
            }
            if ($translated !== false) {
                $this->set_cache($string, $translated);
                return $translated;
            }
            else {
                $this->set_cache($string, $string);
                return $string;
            }
        }
        $this->set_cache($string, $string);
        return $string;
    }

    /**
     * Get the translated phrase from cache, if possible
     *
     * @param string $string Original string
     * @return string Translated string, if exists
     */
    private function from_cache($string) {
        $string = trim($string);
        $key = md5($string);
        if (array_key_exists($key, self::$__cache)) {
            return self::$__cache[$key];
        }
        return false;
    }

    /**
     * Puts the translated string to cache
     *
     * @param string $original The original
     * @param string $translated The translated
     */
    private function set_cache($original, $translated) {
        $original = trim($original);
        $key = md5($original);
        self::$__cache[$key] = $translated;
    }

    /**
     * Check if a language file exist or not
     *
     * @param string $language The language file, for example "vi"
     * @return bool
     */
    private function language_exist($language) {
        return file_exists(LANGUAGE_PATH . $language . "." . LANGUAGE_EXTENSION);
    }

    /**
     * Get the translated phrase from language file
     *
     * @param string $string Original string
     * @param string $language Language
     * @return string Translated string
     */
    private function from_file($string, $language) {
        if (!empty (self::$__file_cache)) {
            $translator = self::$__file_cache;
        }
        else {
            $language_file  = LANGUAGE_PATH . $language . "." . LANGUAGE_EXTENSION;
            $string = trim($string);
            $translator = @file_get_contents($language_file);
            self::$__file_cache = $translator;
        }
        if ( get_magic_quotes_runtime() ) {
            $string = stripcslashes($string);
            $translator = stripcslashes($translator);
        }
        if ($translator === false) {
            return false;
        }
        $string = preg_replace("/\s+/", " ", $string);
        $escape_string = Owl_Format::escape_regex($string);
        $regex = '/'.LANG_OPEN_DELIMITER.'(\s*'.$escape_string.'\s*)'.LANG_MIDDLE_DELIMITER.'(\s*.*\s*)'.LANG_CLOSE_DELIMITER.'/';

        if (preg_match($regex, $translator, $matches)) {
            return trim($matches[2]);
        }
        else {
            return false;
        }
    }

    private function from_user_lang($string, $language, $force = false) {
        $owl_ulang = new Owl_Ulang();
        $translator = $owl_ulang->get_ulang($string);
        if (!empty ($translator)) {
            if (isset ($translator[$language])) {
                return $translator[$language];
            }
            elseif (isset ($translator[get_option('language')]) && !$force) {
                return $translator[get_option('language')];
            }
            else {
                return "";
            }
        }
        else {
            return false;
        }
    }

}

//Translated function

/**
 * Translate function
 *
 * @param string $string Original phrase
 * @return string Translated phrase
 */
function __($string, $ulang = false, $force = false) {
    global $i18n;
    return $i18n->translate($string, $ulang, $force);
}

function get_current_language() {
    global $i18n;
    return $i18n->get_language();
}

